package com.hanlin.backend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hanlin.backend.common.annotation.CodeDate;
import com.hanlin.backend.common.util.ResultCommon;
import com.hanlin.backend.common.util.TableCommon;
import com.hanlin.backend.dto.UserInfo;
import com.hanlin.backend.service.HlUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器  用户
 * </p>
 *
 * @author hl.yuan
 * @since 2020-12-22
 */
@RestController
@Slf4j
@Api(value = "/frontend/api/user", tags = "用户")
public class HlUserController {

    @Autowired
    private HlUserService hlUserService;

    @CodeDate
    @GetMapping("/frontend/api/user/tableList")
    @ApiOperation("获取用户列表")
    public ResultCommon<TableCommon<UserInfo>> getTableList(UserInfo userInfo) {
        IPage<UserInfo> common = hlUserService.getTableList(userInfo);
        return userInfo.build(common);
    }

    @GetMapping("/frontend/api/user/{userId}")
    @ApiOperation("获取用户用户信息")
    public ResultCommon<UserInfo> getUserById(@PathVariable("userId") Integer userId) {
        ResultCommon<UserInfo> common = new ResultCommon();
        try {
            UserInfo userInfo = hlUserService.getUserById(userId);
            common.setData(userInfo);
        } catch (Exception ex) {
            log.error("获取用户失败", ex);
            common.setCode("500");
        }
        return common;
    }

    @PostMapping("/frontend/api/user/save")
    @ApiOperation("获取用户用户信息")
    public ResultCommon<UserInfo> saveUser(@RequestBody UserInfo userInfo) {
        ResultCommon<UserInfo> common = new ResultCommon();
        try {
            hlUserService.userSave(userInfo);
        } catch (Exception ex) {
            log.error("用户保存失败", ex);
            common.setCode("500");
        }
        return common;
    }

    @PostMapping("/frontend/api/user/delete")
    @ApiOperation("获取用户用户信息")
    public ResultCommon<UserInfo> deleteUser(@RequestBody Map<String, Object> param) {
        ResultCommon<UserInfo> common = new ResultCommon();
        try {
            List<Integer> userIds = (List<Integer>) param.get("userIds");
            hlUserService.deleteUser(userIds);
        } catch (Exception ex) {
            log.error("删除失败", ex);
            common.setCode("500");
        }
        return common;
    }

}

